/*
 * Copyright (C) 2021 Huawei Device Co., Ltd.
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 * http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

package com.example.imagesteganographylibrary.view;

import com.example.imagesteganographylibrary.ResourceTable;
import com.example.imagesteganographylibrary.Utils.ResUtils;
import com.example.imagesteganographylibrary.Utils.Utility;
import ohos.agp.components.Component;
import ohos.agp.components.DirectionalLayout;
import ohos.agp.components.LayoutScatter;
import ohos.agp.components.Text;
import ohos.agp.window.dialog.CommonDialog;
import ohos.app.Context;

/**
 * ProgressDialog
 *
 * @ProjectName: Image-Steganography-Library
 * @ClassName: ProgressDialog
 * @Description: ProgressDialog
 * @Author: lr
 * @CreateDate: 2021/5/7 15:19
 * @since 2021-05-07
 */
public class ProgressDialog extends CommonDialog {
    private static final int animTime = 500;
    private static final double widths = 0.9;
    private Text tvIndex;
    private Text tvTitle;

    /**
     * ProgressDialog
     *
     * @param context context
     */
    public ProgressDialog(Context context) {
        super(context);
        init(context);
    }

    /**
     * init
     *
     * @param context context
     */
    private void init(Context context) {
        Component dialogView = LayoutScatter.getInstance(context).parse(ResourceTable.Layout_loading_dialog,
                null, true);
        setContentCustomComponent(dialogView);
        CircleView circleView = (CircleView) dialogView.findComponentById(ResourceTable.Id_circleView);
        circleView.startAnim(animTime);
        tvIndex = (Text) dialogView.findComponentById(ResourceTable.Id_tv_item);
        tvTitle = (Text) dialogView.findComponentById(ResourceTable.Id_tv_title);
        setContentCustomComponent(dialogView);
        setSize((int) (ResUtils.getScreenWidth(context) * widths), DirectionalLayout.LayoutConfig.MATCH_CONTENT);
        setTransparent(false);
        setAutoClosable(true);
    }

    /**
     * showDialog
     *
     * @param title title
     * @param content content
     */
    public void showDialog(String title, String content) {
        if (!Utility.isStringEmpty(title)) {
            tvTitle.setText(title);
        }
        if (!Utility.isStringEmpty(content)) {
            tvIndex.setText(content);
        }
        show();
    }

    /**
     * hideDialog
     */
    public void hideDialog() {
        if (isShowing()) {
            destroy();
        }
    }
}
